package com.miracle.login.module.mvp.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.basemvp.util.ToastUtils;
import com.miracle.common.util.CommonConstants;
import com.miracle.common.util.RadioButtonUtil;
import com.miracle.common.util.S;
import com.miracle.common.util.ToastCommonUtils;
import com.miracle.common.util.Utils;
import com.miracle.login.R;
import com.miracle.login.R2;
import com.miracle.login.module.di.component.DaggerRegisterComponent;
import com.miracle.login.module.di.module.RegisterModule;
import com.miracle.login.module.mvp.contract.RegisterContract;
import com.miracle.login.module.mvp.entity.request.RegisterRequest;
import com.miracle.login.module.mvp.presenter.RegisterPresenter;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * com.miracle.login.module.mvp.ui.activity
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/4 17:02
 * @see com.miracle.login.module.mvp.ui.activity
 */
public class RegisterActivity extends BaseActivity<RegisterPresenter> implements TextWatcher,
        RegisterContract.View {

    @BindView(R2.id.rg_sex)
    RadioGroup mRgSex;
    @BindView(R2.id.il_name)
    TextInputLayout mTILName;
    @BindView(R2.id.il_pwd)
    TextInputLayout mTILPwd;
    @BindView(R2.id.il_rpwd)
    TextInputLayout mTILRpwd;
    @BindView(R2.id.il_tel)
    TextInputLayout mTILTel;
    @BindView(R2.id.il_mail)
    TextInputLayout mTILMail;

    ArrayList<RadioButtonUtil.RadioBean> mSexList = new ArrayList<>();
    ArrayList<RadioButton> mRadioGroupList;

    LifecycleProvider<ActivityEvent> lifecycleProvider;

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        initRadioGroup();
    }

    /**
     * 创建性别radiogroup
     */
    private void initRadioGroup() {
        for (int i = 0; i < CommonConstants.Sex.VALUES.length; i++) {
            RadioButtonUtil.RadioBean bean = new RadioButtonUtil.RadioBean();
            bean.setId(CommonConstants.Sex.IDS[i]);
            bean.setText(CommonConstants.Sex.VALUES[i]);
            mSexList.add(bean);
        }

        mRadioGroupList = RadioButtonUtil.addButtonToRadioGroup(this, mSexList, mRgSex);
        RadioButton rbSex = (RadioButton) mRgSex.getChildAt(0);
        rbSex.setChecked(true);
    }

    @Override
    public void initData() {

    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
        DaggerRegisterComponent
                .builder()
                .appComponent(appComponent)
                .registerModule(new RegisterModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onRequest(LifecycleProvider<ActivityEvent> lifecycleProvider) {
        this.lifecycleProvider = lifecycleProvider;
    }

    /**
     * 注册
     */
    private void submitData() {
        //昵称
        String name = getTextByInputLayout(mTILName);
        //密码
        String pwd = getTextByInputLayout(mTILPwd);
        //确认密码
        String rpwd = getTextByInputLayout(mTILRpwd);
        //电话
        String tel = getTextByInputLayout(mTILTel);
        //邮箱
        String mail = getTextByInputLayout(mTILMail);

        String[] values = {name, pwd, rpwd, tel, mail};
        String[] errors = {"昵称", "密码", "确认密码", "电话", "邮箱"};
        if (Utils.checkNullList(mTILMail, values, errors)) {
            return;
        }

        if (!pwd.equals(rpwd)) {
            S.Short(mTILMail, "两次密码不同").warning().show();
            return;
        }

        RegisterRequest request = new RegisterRequest();
        request.setUserName(name);
        request.setPassWord(pwd);
        request.setPhone(tel);
        request.setEmail(mail);

        request.setSex(RadioButtonUtil.getTagByRadioGroup(mRadioGroupList, mRgSex
                .getCheckedRadioButtonId()) + "");

        mPresenter.register(lifecycleProvider, request);

    }


    @OnClick({R2.id.tv_register})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.tv_register) {
            submitData();
        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    /**
     * 根据layout获取edittext的字符串
     * @param layout
     * @return
     */
    public String getTextByInputLayout(TextInputLayout layout) {
        return layout.getEditText().getText().toString().trim();
    }

    @Override
    public void registerSucc() {
        S.Short(mTILMail, "注册成功").info().show();
    }

    @Override
    public void showData(Object o) {

    }
}
